﻿using BCCommon;
using BCData.OA.AuxiliaryRule;
using BCDto.OA.AuxiliaryRule;
using BCEntity.OA.AuxiliaryRule;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.OA.AuxiliaryRule
{
    public class AuxiliaryRuleService : IAuxiliaryRuleService
    {
        private readonly IAuxiliaryRuleData auxiliaryRuleData;

        public AuxiliaryRuleService(IAuxiliaryRuleData auxiliaryRuleData)
        {
            this.auxiliaryRuleData = auxiliaryRuleData;
        }

        public bool Exists(long auxiliaryRuleId)
        {
            return this.auxiliaryRuleData.Exists(auxiliaryRuleId).GetAwaiter().GetResult();
        }

        public bool Exists(string auxiliaryRuleName)
        {
            return this.auxiliaryRuleData.Exists(auxiliaryRuleName).GetAwaiter().GetResult();
        }

        public AuxiliaryRuleDto Add(AuxiliaryRuleRequestDto requestDto)
        {
            if (this.auxiliaryRuleData.Exists(requestDto.AuxiliaryRuleName).GetAwaiter().GetResult())
            {
                throw new ArgumentException("OA系统辅助规则名称已存在");
            }
            if (requestDto.PageBusinessMappingDepartmentId.HasValue)
            {
                if (!this.auxiliaryRuleData.ExistsByPageBusinessConfigId(requestDto.PageBusinessMappingDepartmentId.Value).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("此辅助规则已存在对应的页面业务与部门映射的配置，无法重复添加");
                }
            }
            var entity = requestDto.As<AuxiliaryRuleEntity>();

            return this.auxiliaryRuleData.Add(entity).GetAwaiter().GetResult().As<AuxiliaryRuleDto>();
        }
        public AuxiliaryRuleDto Update(AuxiliaryRulePutDto putDto)
        {
            if (!this.Exists(putDto.AuxiliaryRuleId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var oldEntity = this.auxiliaryRuleData.GetEntity(putDto.AuxiliaryRuleId).GetAwaiter().GetResult();
            if (oldEntity.AuxiliaryRuleName != putDto.AuxiliaryRuleName)
            {
                if (this.auxiliaryRuleData.Exists(putDto.AuxiliaryRuleName).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("OA系统辅助规则名称已存在");
                }
            }
            if (putDto.PageBusinessMappingDepartmentId.HasValue && oldEntity.PageBusinessMappingDepartmentId != putDto.PageBusinessMappingDepartmentId)
            {
                if (this.auxiliaryRuleData.ExistsByPageBusinessConfigId(putDto.PageBusinessMappingDepartmentId.Value).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("此辅助规则已存在对应的页面业务与部门映射的配置，无法重复添加");
                }
            }
            var entity = putDto.As<AuxiliaryRuleEntity>();
            return this.auxiliaryRuleData.Update(entity).GetAwaiter().GetResult().As<AuxiliaryRuleDto>();
        }

        public bool Delete(long auxiliaryRuleId)
        {
            if (!this.Exists(auxiliaryRuleId))
            {
                throw new ArgumentException("此记录不存在");
            }

            return this.auxiliaryRuleData.Delete(auxiliaryRuleId).GetAwaiter().GetResult();
        }

        public AuxiliaryRuleDto GetEntity(long auxiliaryRuleId)
        {
            return this.auxiliaryRuleData.GetEntity(auxiliaryRuleId).GetAwaiter().GetResult().As<AuxiliaryRuleDto>();
        }

        public AuxiliaryRuleDto GetEntityByPageBusinessConfigId(long pageBusinessMappingDepartmentId)
        {
            return this.auxiliaryRuleData.GetEntityByPageBusinessConfigId(pageBusinessMappingDepartmentId).GetAwaiter().GetResult().As<AuxiliaryRuleDto>();
        }

        public IEnumerable<AuxiliaryRuleDto> GetList()
        {
            return this.auxiliaryRuleData.GetList().GetAwaiter().GetResult().As<IEnumerable<AuxiliaryRuleDto>>();
        }

        public PageableList<AuxiliaryRuleDto> Query(string auxiliaryRuleName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.auxiliaryRuleData.Query(auxiliaryRuleName, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<AuxiliaryRuleDto>
            {
                Items = list.As<IEnumerable<AuxiliaryRuleDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
